import {
  SET_TOKEN,
  DESTROY_TOKEN,
  DESTROY_ACCESS_TOKEN,
} from '../mutation-types'

// initial state
const state = {
  accessToken: window.sessionStorage.accessToken, //访问令牌
  refreshToken: window.localStorage.refreshToken, //刷新令牌
  expiresIn: window.sessionStorage.expiresIn, //访问令牌有效期秒
  createTime: window.sessionStorage.createTime, //令牌创建时间戳
  authenticationStates: (window.sessionStorage.accessToken || false) || (window.localStorage.refreshToken || false), //认证状态
}

// getters
const getters = {}

// actions
const actions = {

}

// mutations
const mutations = {
  [SET_TOKEN](state, token) {
    //本地存储
    console.log(token);
    // debugger;
    window.sessionStorage.accessToken = token.access_token; //访问令牌
    window.localStorage.refreshToken = token.refresh_token; //刷新令牌
    window.sessionStorage.expiresIn = token.expires_in; //访问令牌有效期秒
    window.sessionStorage.createTime = token.create_time; //令牌创建时间戳

    state.accessToken = token.access_token;
    state.refreshToken = token.refresh_token;
    state.expiresIn = token.expires_in;
    state.createTime = token.create_time;
    state.authenticationStates = true;
    // state.authenticationError = '';
  },
  //销毁身份令牌
  [DESTROY_ACCESS_TOKEN](state) {
    window.sessionStorage.removeItem("accessToken");
    window.sessionStorage.removeItem("expiresIn");
    window.sessionStorage.removeItem("createTime");

    state.accessToken = undefined;
    state.expiresIn = undefined;
    state.createTime = undefined;
    state.authenticationStates = false;
  },
  //销毁令牌
  [DESTROY_TOKEN](state) {
    window.sessionStorage.removeItem("accessToken");
    window.localStorage.removeItem("refreshToken");
    window.sessionStorage.removeItem("expiresIn");
    window.sessionStorage.removeItem("createTime");

    state.accessToken = undefined;
    state.refreshToken = undefined;
    state.expiresIn = undefined;
    state.createTime = undefined;
    state.authenticationStates = false;
  },

}

export default {
  state,
  getters,
  actions,
  mutations
}
